<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
if(isset($_SESSION['lang']))
    require_once "./language_files/signIn2_" . $_SESSION['lang'] . ".php";
else
    require_once "./language_files/signIn2_en.php";

if(isset($_SESSION['member_id']) && isset($_SESSION['email']))
{

    if(isset($_GET['val']))
    {
        echo "<script>top.window.location='main.php?page=conferenceSetup.php';</script>";
    }
    else
    {
        echo "<script>top.window.location='main.php';</script>";
    }
}
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <link rel="stylesheet" type="text/css" href="style3.css"/>
        <?php include "jquery.php"?>
        <title></title>
    </head>
    <body>
        <button class="close" id="close" value="<?php echo $lang['close'];?>" onclick="javascript: top.window.location='index.php';"></button>
        <fieldset>
            <legend align="left"><?php echo $lang['signIn'];?></legend>
            <input type="hidden" id="new_user" value="<?php
        if(isset($_GET['user']))
        {
            echo $_GET['user'];
        }
        else
        {
            echo "null";
        }
        ?>"/>
            <table>
                <tr>
                    <td class="no_border"><?php echo $lang['mail'];?></td>
                    <td class="no_border"><input type="text" id="email" name="email" value="<?php
                   if(isset($_COOKIE['cookemail']))
                   {
                       echo $_COOKIE['cookemail'];
                   }
        ?>" /></td>
                </tr>
                <tr>
                    <td class="no_border"><?php echo $lang['pass'];?></td>
                    <td class="no_border">
                        <input type="password" id="password" name="password" value="<?php
                                                 if(isset($_COOKIE['cookpass']))
                                                 {
                                                     echo $_COOKIE['cookpass'];
                                                 }
        ?>"/></td>
                </tr>
                <tr>
                    <td></td>
                    <td colspan="1" class="no_border"> <input id="rememberMe" type="checkbox" name="rememberme" value="0"/><label class="cursor_pointer" id="remMe" for="rememberMe"><?php echo $lang['rememberMe']; ?></label></td>
                    <td class="no_border"><a href="forgetPassword.php"><?php echo $lang['fogotPassword']; ?></a></td>
                </tr>
                <tr class="sign_button">
                    <td class="no_border"></td>
                    <td colspan="1" class="no_border"><button  id="submit" name="submit"><?php echo $lang['submit'];?></button></td>
                    <td colspan="1" class="no_border"><?php echo $lang['notUser'];?><a id="su" href="signUp2.php"><?php echo $lang['signUp'];?></a> <?php echo $lang['now'];?></td>
                </tr>
            </table>
        </fieldset>
        <script type="text/javascript">
            //<![CDATA[
            $(document).ready(function()
            {
                $("#su").bind("click", function(e){
                    e.preventDefault();
                    frame = top.document.getElementById("sign_in");
                    url = $(this).attr("href");
                    $(frame).attr("src", url);
                   $(frame).height(525);
                   $(frame).width(800);
                   $(frame).css("top", 50);
                });
                $("#remMe").bind("click", function(){
                    if( $("#rememberMe").val() == "0") {
                        $("#rememberMe").val("1");
                    } else if( $("#rememberMe").val() == "1") {
                        $("#rememberMe").val("0");
                    }
                });
                if($("#new_user").val()!="null")
                {
                    $.ajax({
                        type:"POST",
                        url:"signInServer.php",
                        data:{new_user:$("#new_user").val()},
                        success:function(response)
                        {
                            if(response=="1")
                                alert("Thank you for confirming your email address you can now proceed to signing in");
                            else if(response=="0")
                                alert("This email was not registered in our database please use the sign up option first");
                            else
                                alert(response);
                        }
                    });
                }
                $("#password").bind("focus",function()
                {
                    $(this).bind("keypress",function(event)
                    {
                        if(event.keyCode==13)
                        {
                            event.preventDefault();
                            $(".close").unbind("click");
                            $(this).blur();
                            $("#submit").trigger("click");
                            $(".close").bind("click",function()
                            {
                                top.window.location="index.php";
                            });
                            return false;
                        }
                    });
                });
                $("#password").bind("blur",function()
                {
                    $(this).unbind("keypress");
                });
                $("#submit").bind("click",function()
                {
                    $(this).attr("disabled", "disabled");
                    if($("#email").val()!="" && $("#password").val()!="")
                    {
                        $.ajax({
                            type:"POST",
                            url:"signInServer.php",
                            data:{email:$("#email").val(),password:$("#password").val(),rememberme:$("#rememberMe").val()},
                            success:function(response)
                            {
                                if(response=="1")
                                {
                                    if(window.location.href.indexOf("val")!=-1)
                                        top.window.location="main.php?page=conferenceSetup.php";
                                    else
                                        top.window.location="main.php";
                                }
                                else if(response=="0")
                                {
                                    alert("Email/password is incorrect");
                                    $("#submit").removeAttr("disabled");
                                }

                                else if(response.indexOf("Account not activated")!=-1)
                                {
                                    if(confirm(response+":: Do you wish for the confirmation email to be resent?"))
                                    {
                                        $.ajax({
                                            type:"POST",
                                            url:"signInServer.php",
                                            data:{confirm_mail:$("#email").val()},
                                            success:function(confirmed)
                                            {
                                                if(confirmed=="1")
                                                    alert("confirmation email has been resent please check your inbox and spam folders");
                                                else
                                                    alert(confirmed);
                                                $("#submit").removeAttr("disabled");
                                            }
                                        });
                                    }
                                }
                                else
                                    alert(response);
                            }
                        });
                    }
                    else
                    {
                        alert("please fill in all input fields");
                        $("#submit").removeAttr("disabled");
                    }
                });
            });
            //]]>
        </script>
    </body>
</html>
